//
//  QSetUpPassWordView.m
//  QoilPublic
//
//  Created by 杨凯 on 16/7/21.
//  Copyright © 2016年 杨凯. All rights reserved.
//

#import "QSetUpPassWordView.h"

@interface QSetUpPassWordView()

@property(nonatomic, weak) UITextField *textField;
@property (nonatomic, strong) NSMutableArray *dataSource;

@end

@implementation QSetUpPassWordView{

    BOOL _isback;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createUI];
    }
    return self;
}

- (void)createUI{

    self.backgroundColor = [UIColor whiteColor];
    self.layer.cornerRadius = 5.0;
    self.layer.borderColor = [QColorUtility defaultLineColor].CGColor;
    self.layer.borderWidth = 1;
    UITextField *textField = [[UITextField alloc] initWithFrame:self.bounds];
    textField.hidden = YES;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    [textField addTarget:self action:@selector(textChange:) forControlEvents:UIControlEventEditingChanged];
    [self addSubview:textField];
    self.textField = textField;
}

- (void)setCount:(NSUInteger)count{
    _count = count;
    for (int i = 0; i < self.count; i++){
        UITextField *pwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(i*(self.bounds.size.width/4), 0,  (self.bounds.size.width/4), self.bounds.size.height)];
        pwdTextField.layer.borderColor = [QColorUtility defaultLineColor].CGColor;
        pwdTextField.enabled = NO;
        pwdTextField.textAlignment = NSTextAlignmentCenter;//居中
        pwdTextField.secureTextEntry = YES;//设置密码模式
        pwdTextField.userInteractionEnabled = NO;
        [self insertSubview:pwdTextField belowSubview:self.textField];
        [self.dataSource addObject:pwdTextField];
    }
    
    for (int i = 1; i < self.count; ++i) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(i*(self.bounds.size.width/4), 0, 0.5, self.bounds.size.height)];
        line.backgroundColor = [QColorUtility defaultLineColor];
        [self addSubview:line];
    }
}

- (void)clearText {
    _isback = YES;
    self.textField.text = nil;
    [self textChange:self.textField];
}

#pragma mark - 文本框内容改变
- (void)textChange:(UITextField *)textField {
    NSString *password = textField.text;
    if (password.length > self.count) {
        return;
    }else{
        for (int i = 0; i < _dataSource.count; i++)
        {
            UITextField *pwdTextField= [_dataSource objectAtIndex:i];
            if (i < password.length) {
                NSString *pwd = [password substringWithRange:NSMakeRange(i, 1)];
                pwdTextField.text = pwd;
            } else {
                pwdTextField.text = nil;
            }
        }

        if (password.length == _dataSource.count){
            [textField resignFirstResponder];//隐藏键盘
        }
        if(!_isback){
           !self.passwordBlock ? : self.passwordBlock(textField.text);
        }
        _isback = NO;
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.textField becomeFirstResponder];//哪个响应键盘，哪个change值就会改变
}

- (NSMutableArray *)dataSource {
    if (_dataSource == nil) {
        _dataSource = [NSMutableArray arrayWithCapacity:self.count];
    }
    return _dataSource;
}

@end
